<?php
namespace app\admin\controller;
use app\common\model\CourseGrade as CourseGradeModel;
use think\validate;
use think\Db;

class CourseGrade extends Base
{
    
    public function index(){
		
		$data['kw'] = input('kw');
		$data['Nowpage'] = input('get.page') ? input('get.page'):1;
		
		$map['status'] = 1;
		if($data['kw']&&$data['kw']!==""){
			$map['title'] = ['like',"%" . $data['kw'] . "%"];
		}
		$list = CourseGradeModel::where($map)->order('id asc')->paginate(config('list_rows'))->toArray();
		$data['count'] = $list['total'];
		$data['allpage'] = $list['last_page'];  
		$user_date = $list['data'];
		
		if(input('get.page')){
			return json($user_date);
		}
		$this->assign($data);
		return $this->fetch();
    }
	
    public function edit(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
			$validate = validate('Common');
			if (!$validate->check($data)) {
				$this->error($validate->getError());
			}
			if ($data['id']) {//编辑
                $res = CourseGradeModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $this->success('编辑成功');
                } else {
                    $this->error('编辑失败');
                }
            } else {//添加
                $obj = new CourseGradeModel;
                $res = $obj->allowField(true)->save($data);
                if ($res) {
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }
            }
        } else {
            $id = $this->request->param('id', 0, 'intval');
            if ($id) {
                $list = CourseGradeModel::where('id', '=', $id)->find();
            }else{
				$list = [];
            }
            return $this->fetch('',['list'=>$list]);
        } 
        
    }
	
	
    public function del(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
			$res = CourseGradeModel::where('id',$id)->delete();
			if ($res) {
				$this->success('删除成功');
			}else {
				$this->error('删除失败');
			}
        } else {
            $this->error('参数错误');
        }
        
    }
    
        
}




?>